--
-- @Author:      name
-- @DateTime:    20189-07-30 23:05:48
-- @Description: 所有游戏桌子共用到的消息

local skynet = require "skynet"
local config = require "configquery"
-- require "common"

-- local AssertEx = AssertEx


local DeskMessage = class("DeskMessage")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function DeskMessage:ctor(message_dispatch)
	self.message_dispatch = message_dispatch
	self.room_config = nil --房间配置
	self:register()
end

--注册本服务里的消息
function DeskMessage:register()
	self.message_dispatch:registerSelf('start', handler(self,self.start))	
    self.message_dispatch:registerSelf('enter', handler(self,self.enterReq))	
    
    self.message_dispatch:registerSelf('leaveDeskReq', handler(self,self.leaveDeskReq))	    
    self.message_dispatch:registerSelf('deskStatusReq', handler(self,self.deskStatusReq))	
    self.message_dispatch:registerSelf('sitDownReq', handler(self,self.sitDownReq))	
    self.message_dispatch:registerSelf('standUpReq', handler(self,self.standUpReq))	
    self.message_dispatch:registerSelf('cotrolReq', handler(self,self.cotrolReq))	

    self.message_dispatch:registerSelf('disconnect', handler(self,self.disconnect))	
end







---------------------------------------------------------
-- CMD
---------------------------------------------------------
--服务初始化
function DeskMessage:start(table_id)
	print("____________game_start_________")
	self.table_id = table_id
	self.config = global.config	
	self.node_message = global.node_message
	self.user_manager = global.user_manager
	self.desk = global.desk
	self.message_handler = global.message_handler


	self.config:start(table_id) --房间配置
	self.desk:start(table_id)
	self.message_handler:start(table_id)
end

--进入桌子
function DeskMessage:enterReq(msg_data)
	print("____enterDeskReq____",msg_data)

    local user_id = msg_data.user_id   
    local table_id = msg_data.table_id 
    local user = self.user_manager:getUser(user_id)
    if not user then --
  --   	--断线重连进来
  --   	print("_____断线重连进来的________")
		-- return {enterDeskRes={
		-- 	error_code = 0,
		-- 	error_msg = "进入桌子成功了",
		-- 	desk_id = user:getDeskId(),
		-- }}
		self.user_manager:addUser(user_id)
		user = self.user_manager:getUser(user_id)
    end
    user:setDisconnect(false)

    -- --在线人数+1
    -- self.server_manager:increaseOnlineCount()
    -- --发送在线信息
    -- self:sendServerInfo()
    if self.desk.enterDeskCallback then 
    	self.desk:enterDeskCallback(user_id)
    end
	return {enterRes={
		table_id = table_id,
	}}
end

--离开
function DeskMessage:leaveDeskReq(msg_data)
    local user_id = msg_data.user_id    
    local user = self.user_manager:getUser(user_id)
    if not user then --玩家不在这个桌子里
    	log.error("_____调用离桌子，但玩家不在这个桌子里________",msg_data)
		return {leaveDeskRes={
			error_code=1,
			error_msg="离桌失败"
		}}
    end
    local res
    if self.desk.leaveDeskCallback then 
    	res = self.desk:leaveDeskCallback(user_id)
    end	
	if res and next(res) then 
		return res
	end
	--站起,删除
	self.user_manager:removeUser(user_id)
	return {leaveDeskRes={
		error_code=0,
		error_msg="离桌成功"
	}}
end

--掉线
function DeskMessage:disconnect(msg_data)
	print("__DeskMessage____disconnect___", msg_data)
    local user_id = msg_data.user_id    
    local user = self.user_manager:getUser(user_id)
    if not user then --玩家不在这个桌子里
    	error_log("_____调用disconnect，但玩家不在这个桌子里________",msg_data)
    end	
    local res
    if self.desk.disconnectCallback then 
    	res = self.desk:disconnectCallback(user_id)
    end		
	if res and next(res) then 
		return res
	end
	--旁观者掉线马上踢出大厅
	--从user_manager中删除
	self.user_manager:removeUser(user_id)
	return {error_code=0}
end

-- 得到桌子状态
function DeskMessage:deskStatusReq(msg_data)
    local user_id = msg_data.user_id    
    local user = self.user_manager:getUser(user_id)
    if not user then --玩家不在这个桌子里
    	log.error("_____调用deskStatusReq，但玩家不在这个桌子里________",msg_data)
		return {deskStatusRes={
			error_code=1,
			error_msg="玩家不在这个桌子里"
		}}
    end
    local res
    if self.desk.deskStatusCallback then 
    	res = self.desk:deskStatusCallback(user_id)
    end		
	--桌子上人数各种共用信息
	if res and next(res) then 
		return res
	end
	return {deskStatusRes={
		error_code = 1,
		error_msg = "取桌子状态错误",
	}}	

end

-- 请求坐下
function DeskMessage:sitDownReq(msg_data)
    local user_id = msg_data.user_id    
    local user = self.user_manager:getUser(user_id)
    if not user then --玩家不在这个桌子里
    	log.error("_____调用sitDownReq，但玩家不在这个桌子里________",msg_data)
		return {deskStatusRes={
			error_code=1,
			error_msg="玩家不在这个桌子里"
		}}
    end
	local seat_id = self.user_manager:sitDown(user_id)
	if seat_id then 
		if self.desk.sitDownCallback then --让各个游戏做它要做的事
			self.desk:sitDownCallback()
		end
		return {sitDownRes={
			error_code = 0,
			seat_id = seat_id,
		}}
	end
	return {sitDownRes={
		error_code = 1,
		error_msg = "坐下错误",
	}}	

end
-- 得到桌子状态
function DeskMessage:standUpReq(msg_data)
    local user_id = msg_data.user_id    
    local user = self.user_manager:getUser(user_id)
    if not user then --玩家不在这个桌子里
    	log.error("_____调用standUpReq，但玩家不在这个桌子里________",msg_data)
		return {deskStatusRes={
			error_code=1,
			error_msg="玩家不在这个桌子里"
		}}
    end
	local seat_id = self.user_manager:standUp(user_id)
	if seat_id then 
		if self.desk.standUpCallback then --让各个游戏做它要做的事
			self.desk:standUpCallback()
		end
		return {standUpRes={
			error_code = 0,
			seat_id = seat_id,
		}}
	end
	return {sitDownRes={
		error_code = 1,
		error_msg = "站起错误",
	}}	

end











--GM命令控制
function DeskMessage:cotrolReq(msg_data)
	-- self.machine:setGmCard(playerid,cards,repeatNum)
end

--更新属性
function DeskMessage:updatePropertyNt(playerid, prop_data)
	print("_________________玩家数据更新___",data)
	return true
end

--广播
function DeskMessage:broadcastNt( pack )
	return true
end

--安全关服
function DeskMessage:safeCloseReq(close)
	self.close = close
end
------------------------------------------------------------------------------------------------
---游戏里的操作
------------------------------------------------------------------------------------------------


return DeskMessage